package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Customer;
import cn.wolfcode.rbac.query.CustomerQuery;
import cn.wolfcode.rbac.service.ICustomerService;
import cn.wolfcode.rbac.service.IEmployeeService;
import cn.wolfcode.rbac.service.ISystemDictionaryItemService;
import cn.wolfcode.rbac.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;


    //部门列表
    //@RequriedPermission(name="部门页面",expression="department:list")
    @RequestMapping("/failList")
    public String failList(@ModelAttribute("qo") CustomerQuery qo, Model model) {
        model.addAttribute("result", this.customerService.query3(qo));
        model.addAttribute("sellers", employeeService.list());
        return "customer/fail_list";
    }

    @RequestMapping("/poolList")
    public String poolList(@ModelAttribute("qo") CustomerQuery qo, Model model) {
        model.addAttribute("result", this.customerService.query2(qo));
        model.addAttribute("sellers", employeeService.list());
        return "customer/pool_list";
    }

    @RequestMapping("/potentialList")
    public String potentialList(@ModelAttribute("qo") CustomerQuery qo, Model model) {
        model.addAttribute("result", this.customerService.query0(qo));
        model.addAttribute("sellers", employeeService.list());
        model.addAttribute("jobs", systemDictionaryItemService.selectJobs());
        model.addAttribute("sources", systemDictionaryItemService.selectSources());
        model.addAttribute("ccts", systemDictionaryItemService.selectCcts());
        return "customer/potential_list";
    }

    @RequestMapping("/realList")
    public String realList(@ModelAttribute("qo") CustomerQuery qo, Model model) {
        model.addAttribute("result", this.customerService.query1(qo));
        model.addAttribute("sellers", employeeService.list());
        return "customer/real_list";
    }

    @RequestMapping("/lossList")
    public String lossList(@ModelAttribute("qo") CustomerQuery qo, Model model) {
        model.addAttribute("result", this.customerService.query4(qo));
        model.addAttribute("sellers", employeeService.list());
        return "customer/loss_list";
    }

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") CustomerQuery qo, Model model) {
        model.addAttribute("result", this.customerService.query(qo));
        model.addAttribute("sellers", employeeService.list());
        return "customer/list";
    }


    //部门在修改之前，需要做查询工作，用作编辑时数据回显
    //@RequriedPermission(name="部门编辑", expression="department:input")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("entity", this.customerService.get(id));
        }
        return "customer/input";
    }

    //部门新增/编辑
    //@RequriedPermission(name="部门新增/编辑", expression="department:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Customer entity) {
        try {
            this.customerService.saveOrUpdate(entity);
            return new JsonResult("操作成功", true);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("操作失败", false);
        }
    }

    //部门删除
    //@RequriedPermission(name="部门删除", expression="department:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        this.customerService.delete(id);
        return "redirect:/customer/list.do";
    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public JsonResult updateStatus(Long customerId, Integer status) {
        try {
            this.customerService.updateStatus(customerId, status);
            return new JsonResult("操作成功", true);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("操作失败", false);
        }
    }


}
